#include <cstdio>
#include <memory.h>

int a[ 20001 ], c1[ 100001 ], c2[ 100001 ], n;

int lowbit( int t )
{
    return t & ( t ^ ( t - 1 ) );
}

int Sum( int c[], int s )
{
    int sum = 0;
    while ( s > 0 )
    {
        sum += c[ s ];
        s -= lowbit( s );
    }
    return sum;
}

void Add( int c[], int s, int p )
{
    while ( s <= 100000 )
    {
        c[ s ] += p;
        s += lowbit( s );
    }
}

void init( )
{
    int i;
    scanf("%d", &n);
    for ( i = 0; i < n; i++ )
        scanf("%d", &a[ i ]);
    memset( c1, 0, sizeof( c1 ) );
    memset( c2, 0, sizeof( c2 ) );
    Add( c1, a[ 0 ], 1 );
    for ( i = 2; i < n; i++ )
        Add( c2, a[ i ], 1 );
}

void work( )
{
    int i;
    __int64 cnt = 0;
    for ( i = 1; i < n - 1; i++ )
    {
        cnt += Sum( c1, a[ i ] ) * ( Sum( c2, 100000 ) - Sum( c2, a[ i ] ) );
        cnt += Sum( c2, a[ i ] ) * ( Sum( c1, 100000 ) - Sum( c1, a[ i ] ) );
        Add( c1, a[ i ], 1 );
        Add( c2, a[ i + 1 ], -1 );
    }
    printf("%I64d\n", cnt);
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        work( );
    }
    return 0;
}
